package com.atxiao.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class MerchantController {

    String fileType=".txt";
    String filepath="/home/local/demo/";
    String filename="allMerList"+fileType;


    @RequestMapping("/addMerchantList2")
    public void addMerchant(@RequestBody JSONObject jsonObject)throws Exception{

        File file = new File(filepath);
        if (!file.exists()){
            file.mkdirs();
            System.out.println("创建文件夹成功~~~");
        }

        File file1 = new File(filepath + filename);
        if (!file1.exists()){
            try {
                file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("创建文件成功~~~");
        }

        //判断商户重复
        List<String> list = this.getMerId();

        String merID = (String) jsonObject.get("merID");
        //结果数组不为空再去进行判断重复
        if (!(list.size()==0)){
            for (String res : list) {
                if (res.equals(merID)){
                    throw new RuntimeException("商户重复");
                }
            }
        }


        StringBuilder sb = new StringBuilder();
        String merchant = jsonObject.toJSONString();
        sb.append(merchant).append(",");
        System.out.println("组装好要添加的信息---"+sb.toString());
        FileWriter fileWriter = new FileWriter(filepath+filename, true);
        fileWriter.write(sb.toString());
        fileWriter.close();

    }





    //获取商户下拉框列表：原本的文件中存的是Json字符串的形式，获取列表的时候应该将文件内容获取出来以后在前后拼接上[],将其转化为一个JSON数组，然后再对其进行操作
    @RequestMapping("/getMerchantList2")
    public List<String> getMerChantMsg()throws Exception{
        ArrayList<String> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        FileInputStream fis = new FileInputStream(filepath + filename);
        String res = IOUtils.toString(fis, "utf-8");
        fis.close();

        StringBuilder sb1 = sb.append("[");
        StringBuilder sb2 = sb1.append(res);
        int start=sb2.length()-1;
        int end=sb2.length();
        StringBuilder sb3 = sb2.replace(start, end, " ");
        StringBuilder sb4 = sb3.append("]");


        System.out.println("拼接后的数组---------------------------"+sb4.toString());
        JSONArray jsonArray = JSONObject.parseArray(sb4.toString());

        for (Object o : jsonArray) {
            JSONObject object= (JSONObject) o;
            String name = (String) object.get("merName");
            list.add(name);
//            list.add(1,name);
        }


        return list;
    }


    //获取文件中的json数组
    @RequestMapping("/delete")
    public void getjsonArray(@RequestBody JSONObject jsonObject)throws Exception{
        StringBuilder sb = new StringBuilder();
        FileInputStream fis = new FileInputStream(filepath + filename);
        String res = IOUtils.toString(fis, "utf-8");
        fis.close();

        StringBuilder sb1 = sb.append("[");
        StringBuilder sb2 = sb1.append(res);
        int start=sb2.length()-1;
        int end=sb2.length();
        StringBuilder sb3 = sb2.replace(start, end, "");
        StringBuilder sb4 = sb3.append("]");


        System.out.println("拼接后的数组---------------------------"+sb4.toString());
        JSONArray jsonArray = JSONObject.parseArray(sb4.toString());

        boolean flag = jsonArray.remove(jsonObject);
        if (flag){
        System.out.println("删除成功");
        }
        //继续存储一次，因为转出来的JsonArray是存放在内存中的
        String res2 = jsonArray.toJSONString();
        //去掉json数组的[
        String replace_res = res2.replace("[", " ");
        //去掉json数组的]
        String replace_res2 = replace_res.replace("]", " ");
        StringBuilder builder = new StringBuilder(replace_res2);
        System.out.println("拼接要去存储的信息："+builder);
        //在末尾追加一个,
        builder.append(",");
        //将删除后的信息再次存放到文件中
        FileWriter fileWriter = new FileWriter(filepath+filename, false);
        fileWriter.write(builder.toString());
        fileWriter.close();


    }




    public List<String> getMerId() throws  Exception{
        ArrayList<String> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        FileInputStream fis = new FileInputStream(filepath + filename);
        if (fis.available()==0){
            return list;
        }
            String res = IOUtils.toString(fis, "utf-8");
            fis.close();

            StringBuilder sb1 = sb.append("[");
            StringBuilder sb2 = sb1.append(res);
            int start=sb2.length()-1;
            int end=sb2.length();
            StringBuilder sb3 = sb2.replace(start, end, " ");
            StringBuilder sb4 = sb3.append("]");


            System.out.println("拼接后的数组---------------------------"+sb4.toString());
            JSONArray jsonArray = JSONObject.parseArray(sb4.toString());

            for (Object o : jsonArray) {
                JSONObject object= (JSONObject) o;
                String merID = (String) object.get("merID");
                list.add(merID);
//            list.add(1,name);
            }

        return list;

        }



    @RequestMapping("/deleteMarchant")
    public void deleteMerChant(@RequestBody JSONObject jsonObject) throws Exception{
        //获取参数传递的merID
        String merID = (String) jsonObject.get("merID");
        List<String> merIdList = this.getMerId();




    }






}
